package com.inossem.allotin.adapter.input;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.inossem.allotin.R;
import com.inossem.allotin.utils.Utils;
import com.inossem.wms.model.biz.BizReceiptAttachmentVo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 提交Dialog
 *
 * @author 詹建宇
 * @time on 2019/1/26 15:21
 * @email jianyu.zhan@inossem.com
 */
public class SubmitDialogPicAdapter extends BaseAdapter {

    private Context context;
    private List<BizReceiptAttachmentVo> list;
    private boolean isCanEdit;
    private Integer choosePosition = null;
    private OnItemClickListener listener = null;

    public SubmitDialogPicAdapter(Context context, List<BizReceiptAttachmentVo> list) {
        this.context = context;
        this.list = list;
        this.isCanEdit = false;
    }

    public SubmitDialogPicAdapter(Context context, List<BizReceiptAttachmentVo> list, boolean isCanEdit) {
        this.context = context;
        this.list = list;
        this.isCanEdit = isCanEdit;
    }

    public List<BizReceiptAttachmentVo> getList() {
        return list;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    public int getListSize() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_dialog_submit_pic_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (null != choosePosition && position == choosePosition) {
            holder.img.setImageResource(R.drawable.ic_delete_b);
            if (isCanEdit) {
                holder.img.setOnClickListener(v -> {
                    list.remove(position);
                    choosePosition = null;
                    if (null != listener) listener.onItemClick(0);
                    notifyDataSetChanged();
                });
                holder.img.setOnLongClickListener(v -> {
                    Utils.showSubmitImg(holder.img, list.get(position).getFileCode());
                    choosePosition = null;
                    notifyDataSetChanged();
                    return true;
                });
            }
        } else {
            Utils.showSubmitImg(holder.img, list.get(position).getFileCode());
            if (isCanEdit) {
                holder.img.setOnClickListener(v -> {
                    if (null != listener) {
                        listener.onItemClick(1);
                    }
                });
                holder.img.setOnLongClickListener(v -> {
                    holder.img.setImageResource(R.drawable.ic_delete_b);
                    choosePosition = position;
                    notifyDataSetChanged();
                    return true;
                });
            }
        }

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.img)
        ImageView img;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int type);
    }

}
